﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Xml;

namespace DinoMessenger
{
    public class WSMessage {
        public String Text { get; set; }
        public String Crypted { get; set; }
    }

    /*
    public class WSResponse{
        public WSMessage[] Message;
        public WSResponse()
        {
            Message = new WSMessage[10];
        }
    }
     * */

    class WSHandler
    {
        private String lsWSEndpoint;
        private List<WSMessage> lrXMLResponse;
        //private EncodingClass lrEncode;

        public WSHandler()
        {
            // init
            //lrEncode = new EncodingClass();
        }

        private XmlDocument CallWS(String isEndPoint, String isGetParams = "") {
            String lsRequest = isEndPoint + "?input=" + isGetParams;
            //
            HttpWebRequest req = WebRequest.Create(isEndPoint) as HttpWebRequest;
            System.Console.WriteLine(lsRequest);
            //
            XmlDocument xmlDoc = new XmlDocument();
            using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
            {
                xmlDoc.Load(resp.GetResponseStream());
            }
            return xmlDoc;
        }

        private void ParseXMLMessages(XmlDocument lrXMLDocument)
        {
            System.Xml.XmlNode lrValueNode;
            lrXMLResponse = new List<WSMessage>();
            //
            System.Xml.XmlNodeList CNodes = lrXMLDocument.SelectNodes("getMessagesResponse/Message");

            /* projit jednotlive zpravy a nacpat do objektu */
            foreach (System.Xml.XmlNode node in CNodes)
            {
                WSMessage lrWsMessage = new WSMessage();
                lrValueNode = node.SelectSingleNode("text");
                lrWsMessage.Text = lrValueNode.InnerText;
                //System.Console.WriteLine("lrWsMessage[{0}].Text: {1}", i, lrXMLResponse[i].Text);

                lrValueNode = node.SelectSingleNode("crypted");
                lrWsMessage.Crypted = "N";
                //
                lrXMLResponse.Add(lrWsMessage);
            }
        }

        public List<WSMessage> getMessages()
        {
            String lsParams;
            lsParams = "<getMessagesRequest><idclient>10002</idclient></getMessagesRequest>";
            ParseXMLMessages(CallWS("http://mikulas.php5.cz/ws/WSGetMessages.php", EncodingClass.Base64ForUrlEncode(lsParams)));
            return lrXMLResponse;
        }

        public void SendMessage(String isMessage, Boolean ibCrypted) {
            XmlDocument lrXMLDocument;
            String lsParams;
            lsParams = "<sendMessageRequest><idclient>10002</idclient><idclientto>10002</idclientto><crypted>N</crypted><message>" + isMessage + "</message></sendMessageRequest>";
            System.Console.WriteLine(lsParams);
            System.Console.WriteLine(EncodingClass.Base64ForUrlEncode(lsParams));
            lrXMLDocument = CallWS("http://mikulas.php5.cz/ws/WSSendMessage.php", EncodingClass.Base64ForUrlEncode(lsParams));
        }
    }
}
